package com.jrx.anytxn.param.job.bactchBillCompare.step;

import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.config.BillCompareConfig;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.TlBillTransCompareDetail;
import com.jrx.anytxn.param.entity.TlThirdBillDetail;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author likai.lu
 */
@Service
public class ThirdDetailUnmatchedProcessor implements ItemProcessor<TlThirdBillDetail, TlBillTransCompareDetail> {

    @Autowired
    private ISegmentService iSegmentService;
    @Autowired
    private BillCompareConfig billCompareConfig;
    private org.slf4j.Logger logger = LoggerFactory.getLogger(ThirdDetailUnmatchedProcessor.class);

    @Override
    public TlBillTransCompareDetail process(TlThirdBillDetail item) throws Exception {
        if (StringUtils.isBlank(item.getCoreSeq()) && StringUtils.isBlank(item.getCoreBusDate())) {
            logger.warn("该{}明细未匹配到TXN交易记录,且没有T24系统流水号和T24系统业务日期", item.getThirdSeq());
            return null;
        }
        TlBillTransCompareDetail tlBillTransCompareDetail = new TlBillTransCompareDetail();
        tlBillTransCompareDetail.setId(iSegmentService.getId("TlBillTransCompareDetail"));
        tlBillTransCompareDetail.setTenantId(item.getTenantId());
        tlBillTransCompareDetail.setOrganizationId(item.getOrganizationId());
        tlBillTransCompareDetail.setBillType(this.buildBillType(item.getTransactionType(), item.getThirdChannel()));
        tlBillTransCompareDetail.setCompareStatus(Constant.COMPARE_FLAG_0);
        tlBillTransCompareDetail.setCompareDesc("该明细未匹配到TXN交易记录");
        tlBillTransCompareDetail.setCustomerId("");
        tlBillTransCompareDetail.setLoanNumber("");
        tlBillTransCompareDetail.setSeqNo(item.getThirdSeq());
        tlBillTransCompareDetail.setThirdChannel(billCompareConfig.getThirdChannel());
        tlBillTransCompareDetail.setDetailAmount(item.getTransactionAmount());
        tlBillTransCompareDetail.setTxnPostingAmnt(null);
        tlBillTransCompareDetail.setBillTransStatus(item.getTransStatus());
        tlBillTransCompareDetail.setTxnTransStatus(null);
        tlBillTransCompareDetail.setBusinessDate(item.getBusinessDate());
        return tlBillTransCompareDetail;
    }

    /**
     * 账单类型
     * @param transactionType
     * @param thirdChannel
     * @return
     */
    private String buildBillType(String transactionType, String thirdChannel) {
        boolean contains = Arrays.asList("PCP", "T24").contains(thirdChannel);
        String billType = transactionType;
        if (StringUtils.isNotBlank(transactionType) && contains) {
            if ("I".equalsIgnoreCase(transactionType)) {
                billType = "04";
            } else if ("O".equalsIgnoreCase(transactionType)) {
                billType = "02";
            } else {
                billType = "06";
            }
        }
        return billType;
    }
}
